/*!
 * @Author: Tim Elery
 * @Description: This is the header file for the KVH library
 * @File: kvh_cg5100.hpp
 * @License: GPL
 */

// Include system headers
#include <netdb.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>

// Define COM port
#define COM_PORT				"/dev/ttyUSB0" 

// Define baud rate
#define BAUDRATE				B115200

// Define serial port timeout
#define SERIAL_TIMEOUT_SEC		0
#define SERIAL_TIMEOUT_USEC		100000

// Define serial port buffer size
#define SERIAL_BUFFER_SIZE		256

// Define packet buffer size
#define BUFFER_SIZE				65536

// Define Data packet constants
#define HEADER					0xFE81FF55	// Every message starts with this header
#define IMU_PACKET_SIZE			36 			// Every message is 36 bytes in length excluding start and stop bits 

// Type definitions
typedef struct {
	unsigned char header;
	//float data[MTLEN]; //TODO: Resolve unidentifiable message
	unsigned char checksum;
} IMUpacket;

typedef union {
    float floatValue;
    unsigned char byteValue[4]; 
} ufloat;

typedef union {
    long int longIntValue;
    unsigned char byteValue[4]; 
} ulongint;

typedef struct
{
    // IMU data
    float xEulerAngle;      // radians
    float yEulerAngle;      // radians
    float zEulerAngle;      // radians
	float xAccelerometer;   // m/s^2
    float yAccelerometer;   // m/s^2
    float zAccelerometer;   // m/s^2
	float OdometerPulses;	// 45 khz
	bool StatusMessage;		// 1=valid, 0=invalid
	char SequenceNumber;	// 0-127
	
} IMUdata;

namespace KVH
{
    class KVH
    {
        private:
        int serialPort;             //This contains the file descriptor of the opened port
        public:
        KVH();                      //Constructor
        ~KVH();                     //Destructor
        void initSerial (void);
        int readSerial (unsigned char packetBuffer[]);
        int decodeIMU (IMUdata *data);
    };
}
